//-------------------------------------------------
//		Quick Pager jquery plugin
//		Created by dan and emanuel @geckonm.com
//		www.geckonewmedia.com
// 
//
//		18/09/09 * bug fix by John V - http://blog.geekyjohn.com/
//		1.2 - allows reloading of pager with new items
//		
//		수정사항
//		1. 이전, 다음 버튼 추가 (http://chanlee.tistory.com/39) 참고함
//			- 기본 뼈대는 비슷하며, jquerymobile 연동시 문제가 발생하여 그 부분만 수정함.
//-------------------------------------------------

(function($) {
	$.fn.quickPager = function(options) {
	
		var defaults = {
			pageSize: 10,
			listSize: 5,
			currentPage: 1,
			holder: null,
			prevText : '이전',
			nextText : '다음',
			pagerLocation: "after"
		};
		
		var options = $.extend(defaults, options);
		
		
		return this.each(function() {
			var selector = $(this);	
			var pageCounter = 1;
			
			selector.wrap("<div class='simplePagerContainer'></div>");
			
			selector.parents(".simplePagerContainer").find("ul.simplePagerNav").remove();
			
			selector.children().each(function(i){ 
					
				if(i < pageCounter*options.pageSize && i >= (pageCounter-1)*options.pageSize) {
					$(this).addClass("simplePagerPage"+pageCounter);
				}
				else {
					$(this).addClass("simplePagerPage"+(pageCounter+1));
					pageCounter ++;
				}	
				
			});
			
			// show/hide the appropriate regions 
			selector.children().hide();
			selector.children(".simplePagerPage"+options.currentPage).show();
			
			if(pageCounter <= 1) {
				return;
			}
			
			//Build pager navigation
			// prev, next 가 존재하지 않아서 추가함.
			var ps = Math.ceil(selector.children().length / options.listSize);	// ps
			var pageNav = "<ul class='simplePagerNav'>";
			
			for (i=1;i<=pageCounter;i++){
				if (i==options.currentPage) {
					pageNav += "<li class='currentPage simplePageNav"+i+"'><a rel='"+i+"' href='#'>"+i+"</a></li>";	
				}
				else {
					pageNav += "<li class='simplePageNav"+i+"'><a rel='"+i+"' href='#'>"+i+"</a></li>";
				}
				
				// prev next..
				if (0 == (i % options.listSize) && i != pageCounter) {
					pageNav += "<li class='next-"+(i+1)+"'><a rel='"+(i+1)+"' href='#'>"+options.nextText+"</a></li>";
					pageNav += "<li class='prev-"+i+"'><a rel='"+i+"' href='#'>"+options.prevText+"</a></li>";
				}
			}
			
			pageNav += "</ul>";
			
			if(!options.holder) {
				switch(options.pagerLocation)
				{
				case "before":
					selector.before(pageNav);
				break;
				case "both":
					selector.before(pageNav);
					selector.after(pageNav);
				break;
				default:
					selector.after(pageNav);
				}
			}
			else {
				$(options.holder).append(pageNav);
			}
			
			
			// 페이징 관련 부분 모두 숨김
			var simplePagerNav = selector.parent().find(".simplePagerNav");
			simplePagerNav.children().hide();
			
			// 현재 페이징에 위한 한 곳만 show
			var pagingstart = (options.currentPage != 1) ? ((options.currentPage / options.listSize) * options.listSize) + 1 : 1;
			if(!(options.currentPage % options.listSize))
				pagingstart -= options.listSize;
			
			var pagingend = (pagingstart + options.listSize) - 1;
			if(pagingend > ps) 
				pagingend = ps;
			
			for(i=pagingstart; i<=pagingend; i++) {
				if (i == pagingend) {
					simplePagerNav.find('.prev-'+i).hide();
					simplePagerNav.find('.next-'+(i+1)).show();
				} else if (i == pagingstart) {
					simplePagerNav.find('.next-'+(i+1)).hide();
					simplePagerNav.find('.prev-'+(i-1)).show();
				}
				
				simplePagerNav.find('.simplePageNav'+i).show();
			}
			
			//pager navigation behaviour
			selector.parent().find(".simplePagerNav a").click(function() {
					
				//grab the REL attribute 
				var clickedLink = $(this).attr("rel");
				options.currentPage = clickedLink;
				
				
				var simplePagerNavHolder = $(this).parent("li").parent("ul").parent(".simplePagerContainer").find(".simplePagerNav");
				
				// holder관련해서 테스트 안해봄;;;
				if(options.holder) {
					simplePagerNavHolder = $(this).parent("li").parent("ul").parent(options.holder);
					simplePagerNavHolder.find("li.currentPage").removeClass("currentPage");
					simplePagerNavHolder.find("a[rel='"+clickedLink+"']").parent("li").addClass("currentPage");
					//$(this).parent("li").parent("ul").parent(options.holder).find("li.currentPage").removeClass("currentPage");
					//$(this).parent("li").parent("ul").parent(options.holder).find("a[rel='"+clickedLink+"']").parent("li").addClass("currentPage");
				}
				else {
					//remove current current (!) page
					simplePagerNavHolder.find("li.currentPage").removeClass("currentPage");
					//Add current page highlighting
					simplePagerNavHolder.find("a[rel='"+clickedLink+"']").parent("li").addClass("currentPage");
				}
				
				if($(this).text()== options.prevText) {
					end = Math.ceil(clickedLink / options.listSize) * options.listSize;
					start = end - options.listSize+1;
					
					simplePagerNavHolder.children().hide();
					
					for (i=start; i<=end; i++) {
						if (i == end) {
							simplePagerNavHolder.find('.prev-'+i).hide();
							simplePagerNavHolder.find('.next-'+(i+1)).show();
						} else if (i == start) {
							simplePagerNavHolder.find('.next-'+(i+1)).hide();
							simplePagerNavHolder.find('.prev-'+(i-1)).show();
						}
						
						simplePagerNavHolder.find('.simplePageNav'+i).show();		
					}
				} else if ($(this).text() == options.nextText) {
					start = Math.floor(clickedLink / options.listSize) * options.listSize + 1;
					end = start + (options.listSize-1);
					simplePagerNavHolder.children().hide();
					
					for (i=start; i<=end; i++) {
						if (i == end) {
							simplePagerNavHolder.find('.prev-'+i).hide();
							simplePagerNavHolder.find('.next-'+(i+1)).show();
						} else if (i == start) {
							simplePagerNavHolder.find('.next-'+(i+1)).hide();
							simplePagerNavHolder.find('.prev-'+(i-1)).show();
						}		
						simplePagerNavHolder.find('.simplePageNav'+i).show();	
					}
				}
				
				//hide and show relevant links
				selector.children().hide();			
				selector.find(".simplePagerPage"+clickedLink).show();
				
				return false;
			});
		});
	}
	

})(jQuery);
